package q5_blocking_queue_test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements Runnable{
    @Override
    public void run() {

    }
}

//有Runnable，为什么还要有Callable？
class MyThread2 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+ "\t######### come in Callable");
        TimeUnit.SECONDS.sleep(3);
        return 1024;
    }
}

public class CallableDemo {

    public static void main(String[] args) throws Exception {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread2());
//        FutureTask<Integer> futureTask2 = new FutureTask<>(new MyThread2());

        //相同的futureTask放到多个线程里，只会有一个线程执行
        Thread t1 = new Thread(futureTask, "AA");
        Thread t2 = new Thread(futureTask, "BB");
        t1.start();
        t2.start();

        System.out.println(Thread.currentThread().getName() + "************");

        int result = 100;
        int ret = futureTask.get();

        System.out.println("######### result:" + (ret + result));
    }

}
